package com.logistics.modules.warehouseManagement.service.impl;

import cn.hutool.core.convert.Convert;
import com.logistics.modules.transportationManagement.dao.CargoDao;
import com.logistics.modules.transportationManagement.dao.ClientorderDao;
import com.logistics.modules.transportationManagement.entity.CargoEntity;
import com.logistics.modules.transportationManagement.entity.ClientorderEntity;
import com.logistics.modules.transportationManagement.entity.MoveplandetailsEntity;
import com.logistics.modules.warehouseManagement.dao.RepositoryDao;
import com.logistics.modules.warehouseManagement.entity.RepositoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.common.utils.PageUtils;
import com.logistics.common.utils.Query;

import com.logistics.modules.warehouseManagement.dao.SeatDao;
import com.logistics.modules.warehouseManagement.entity.SeatEntity;
import com.logistics.modules.warehouseManagement.service.SeatService;
import org.springframework.transaction.annotation.Transactional;


/**
 * 座位服务impl
 *
 * @author 水穷处云起时
 * @date 2022/05/23
 */
@Service("seatService")
public class SeatServiceImpl extends ServiceImpl<SeatDao, SeatEntity> implements SeatService {

    @Autowired
    private SeatDao seatDao;
    @Autowired
    private CargoDao cargoDao;
    @Autowired
    private ClientorderDao clientorderDao;
    @Autowired
    private RepositoryDao repositoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeatEntity> page = new Query<SeatEntity>().getPage(params);
        // 转换参数
        Integer storehouse = Convert.toInt(params.get("storehouse"));
        String seatName = Convert.toStr(params.get("seatName"));
        //查出数据
        seatDao.selectAll(page,storehouse,seatName);
        return new PageUtils(page);
    }


    @Override
    public PageUtils queryPageByStatus(Map<String, Object> params) {
        IPage<SeatEntity> page = new Query<SeatEntity>().getPage(params);
        // 转换参数
        Integer storehouse = Convert.toInt(params.get("storehouse"));
        String seatName = Convert.toStr(params.get("seatName"));
        //查出数据
        seatDao.selectAllByStatus(page,storehouse,seatName);
        return new PageUtils(page);
    }

    @Override
    public Boolean checkIfASeatIsVacant(CargoEntity cargoEntity, Integer clientOrderId) {
        ClientorderEntity clientorderEntity = clientorderDao.selectOneById(clientOrderId);
        // 根据起始地去数据库看看有多少个仓库
        List<RepositoryEntity> repositoryEntities = repositoryDao.selectAllByStoreAddress(clientorderEntity.getStartAddress());
        for (RepositoryEntity repositoryEntity : repositoryEntities) {
            int count = seatDao.countByRepositoryIdAndCargoTypeId(repositoryEntity.getId(), cargoEntity.getCargoTypeId());
            if (count <= 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void removeByRepositoryIds(List<Integer> asList) {
        baseMapper.deleteByRepositoryIds(asList);
    }

    /**
     * 释放席位占用状态
     *
     * @param details 细节
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void releaseOccupation(MoveplandetailsEntity details) {
        try {
            this.baseMapper.updateCargoIdById(details.getSeatId());
            this.baseMapper.updateCargoSaveSeatById(details.getSeatId());
            this.baseMapper.updateSeatStatusById(0, details.getSeatId());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("运输计划-结算-释放席位的占用状态时失败! 请联系管理员!");
        }
    }

    @Override
    public List<SeatEntity> selectByRepository() {
        return this.baseMapper.selectAllByRepositoryId(11);
    }

}